﻿using BCGM.MedicalWisdom.IServices;
using BCGM.MedicalWisdom.Model;
using BCGM.MedicalWisdom.Model.Models;
using BCGM.MedicalWisdom.Model.ViewModels.Order;
using BCGM.MedicalWisdom.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;

namespace BCGM.MedicalWisdom.Api.Controllers
{
    /// <summary>
    /// 后台管理系统-药品订单详情模块
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize(Permissions.Name)]
    public class DrugOrderItemController : ControllerBase
    {
        /// <summary>
        /// 服务器接口，因为是模板生成，所以首字母是大写的，自己可以重构下
        /// </summary>
        private readonly IDrugOrderItemServices _drugOrderItemServices;

        public DrugOrderItemController(IDrugOrderItemServices DrugOrderItemServices)
        {
            _drugOrderItemServices = DrugOrderItemServices;
        }

        [HttpGet]
        public async Task<MessageModel<PageModel<DrugOrderItem>>> Get(int page = 1, string key = "", int intPageSize = 50)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
            {
                key = "";
            }

            Expression<Func<DrugOrderItem, bool>> whereExpression = a => true;

            return new MessageModel<PageModel<DrugOrderItem>>()
            {
                msg = "获取成功",
                success = true,
                response = await _drugOrderItemServices.QueryPage(whereExpression, page, intPageSize)
            };

        }

        [HttpGet("{id}")]
        public async Task<MessageModel<DrugOrderItem>> Get(string id)
        {
            return new MessageModel<DrugOrderItem>()
            {
                msg = "获取成功",
                success = true,
                response = await _drugOrderItemServices.QueryById(id)
            };
        }

        /// <summary>
        /// 添加药品订单详细表数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Post([FromBody] List<DrugOrderItem> request)
        {
            var data = new MessageModel<string>();

            var id = await _drugOrderItemServices.Add(request);
            data.success = id > 0;
            if (data.success)
            {
                data.response = id.ObjToString();
                data.msg = "添加成功";
            }

            return data;
        }

        [HttpPut]
        public async Task<MessageModel<string>> Put([FromBody] DrugOrderItem request)
        {
            var data = new MessageModel<string>();
            data.success = await _drugOrderItemServices.Update(request);
            if (data.success)
            {
                data.msg = "更新成功";
                data.response = request?.DrugOrderItemId.ObjToString();
            }

            return data;
        }

        [HttpDelete]
        public async Task<MessageModel<string>> Delete(int id)
        {
            var data = new MessageModel<string>();
            var model = await _drugOrderItemServices.QueryById(id);
            model.IsDeleted = true;
            //data.success = await _departmentServices.Update(model);
            if (data.success)
            {
                data.msg = "删除成功";
                data.response = model?.DrugOrderItemId.ObjToString();
            }

            return data;
        }

        /// <summary>
        /// 药品订单详情显示
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<List<DrugOrderItemOutputDto>>> GetOrderItemSql(int orderId)
        {
            return new MessageModel<List<DrugOrderItemOutputDto>>()
            {
                msg = "获取成功",
                success = true,
                response = await _drugOrderItemServices.GetOrderItemSql(orderId),
            };
        }
    }
}